/**
 * 
 * 轮播图的代码
 * 
 */

/**
 * 动画函数
 * 
 */
function animate(element, target) {
  var current = element.offsetLeft;
  clearInterval(element.timer);
  element.timer = setInterval(function () {
    var step = (target - current) / 10;
    // step = current < target ? step : -step
    current += step;
    if (Math.abs(target - current) > Math.abs(step)) {
      element.style.left = current + "px";
    } else {
      clearInterval(element.timer);
      element.style.left = target + "px";
    }
  }, 20)

}

//  获取元素
window.onload = function () {
  var oRotationchart = document.querySelector(".Rotation-chart");
  //获取banner
  var obanner = document.querySelector(".banner");
  //获取banner的宽度
  var obannerWidth = obanner.offsetWidth;
  //获取ul
  var oUl = document.querySelector(".banner ul");
  //获取所有的li
  var ali = oUl.children;
  //声明一个计数器
  var num = 0;
  var oa = document.querySelectorAll(".Rotation-chart .control a")

  /**--------------自动播放和小圆点导航 */
  var timerId = setInterval(function () {
    if (num == ali.length - 1) {
      num = 0;
      oUl.style.left = 0 + 'px';
    }
    num++;
    animate(oUl, -num * obannerWidth);
    if (num > oa.length - 1) {
      oa[oa.length - 1].className = "";
      oa[0].className = "active";
    } else {
      for (var j = 0; j < oa.length; j++) {
        oa[j].removeAttribute("class");
      }
      console.log(num)
      oa[num].className = "active";
    }
  }, 2000)

  for (var i = 0; i < oa.length; i++) {
    oa[i].setAttribute("data-index", i);
    oa[i].onmouseover = function () {
      for (var j = 0; j < oa.length; j++) {
        oa[j].removeAttribute("class");
      }
      this.className = "active";
      var index = this.getAttribute("data-index");
      console.log(index)
      animate(oUl, -index * 1518)
    }
  }
  /**------------鼠标移入移出----------- */
  oRotationchart.onmouseover = function () {
    clearInterval(timerId)
  }
  oRotationchart.onmouseout = function () {
    timerId = setInterval(function () {
      if (num == ali.length - 1) {
        num = 0;
        oUl.style.left = 0 + 'px';
      }
      num++;
      animate(oUl, -num * obannerWidth);
      if (num > oa.length - 1) {
        oa[oa.length - 1].className = "";
        oa[0].className = "active";
      } else {
        for (var j = 0; j < oa.length; j++) {
          oa[j].removeAttribute("class");
        }
        console.log(num)
        oa[num].className = "active";
      }
    }, 2000)
  }


  // 商品部分的tab切换
  /**
   * 1.获取所有的li标签
   * 
   */
  var apriceLi = document.querySelectorAll(".Price-out-nav li");
  var opriceSel = document.querySelectorAll(".content .selection");
  for (var i = 0; i < apriceLi.length; i++) {
    apriceLi[i].setAttribute("data-index", i);
    apriceLi[i].onmouseover = function () {
      for (var j = 0; j < opriceSel.length; j++) {
        apriceLi[j].removeAttribute("class");
        opriceSel[j].style.display = "none";
      }
      this.className = "active";
      opriceSel[this.getAttribute("data-index")].style.display = "block"
    }
  }

  // 秒杀

  function ShowCountDown(year, month, day, divname) {
    var now = new Date();
    var endDate = new Date(year, month - 1, day);
    var leftTime = endDate.getTime() - now.getTime();
    var dd = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    var hh = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时数
    var mm = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟数
    var ss = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
    dd = checkTime(dd);
    hh = checkTime(hh);
    mm = checkTime(mm);
    ss = checkTime(ss); //小于10的话加0
    var cc = document.getElementsByClassName(divname);
    for (var i = 0; i < cc.length; i++) {
      cc[i].innerHTML = dd + "&nbsp;" + ":" + hh + "&nbsp;" + ":" + mm + "&nbsp;" + ":" + ss;
    }

  }

  function checkTime(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  }

  /**
   * 
   * 解决页面刷新滚到顶部 方案
   */
  window.setInterval(function () {
    ShowCountDown(2019, 01, 19, 'countdown2');
  }, 0)


  function FloatDiv() {
    document.getElementById("Hid_ScrollTop").value = document.documentElement.scrollTop;
  }

  window.onscroll = FloatDiv;


  function SetScrollPostion() {
    var vScrollTop = document.getElementById("Hid_ScrollTop").valueif(vScrollTop != "")
    scrollBy(0, vScrollTop); //从顶部自动滚动回刷新前的位置
  }

}


/**
 * 
 * jQuery代码
 * 
 */
$(function () {
  $(".selection1").mouseover(function () {
    $(".selection1-menu").stop().show(200)
  }).mouseout(function () {
    $(".selection1-menu").stop().hide(200)
  })

  $(".selection2").mouseover(function () {

    $(".selection2-menu").stop().fadeIn(200);

  }).mouseout(function () {
    $(".selection2-menu").stop().fadeOut(200)
  })

  $(".selection3").mouseover(function () {
    $(".selection3-menu").stop().show(200)
  }).mouseout(function () {
    $(".selection3-menu").stop().hide(200)
  })

  $(".selection4").mouseover(function () {
    $(".selection4-menu").stop().show(200)
  }).mouseout(function () {
    $(".selection4-menu").stop().hide(200)
  })
})